package com.graduation.springbootinit.service.impl;

import com.graduation.springbootinit.common.BaseResponse;
import com.graduation.springbootinit.common.ErrorCode;
import com.graduation.springbootinit.common.ResultUtils;
import com.graduation.springbootinit.mapper.PlatformMonthlyIncomeMapper;
import com.graduation.springbootinit.model.entity.ShopPart.PlatformMonthlyIncome;
import com.graduation.springbootinit.service.PlatformMonthlyIncomeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class PlatformMonthlyIncomeServiceImpl implements PlatformMonthlyIncomeService {

    @Resource
    private PlatformMonthlyIncomeMapper platformMonthlyIncomeMapper;

    @Override
    public void calculateAndUpdateMonthlyIncome(Integer year, Integer month) {
        // 计算总佣金收入
        BigDecimal totalCommission = platformMonthlyIncomeMapper.calculateTotalCommission(year, month);
        if (totalCommission == null) {
            totalCommission = BigDecimal.ZERO;
        }

        // 计算活跃商铺数
        Integer activeStores = platformMonthlyIncomeMapper.countActiveStores(year, month);
        if (activeStores == null) {
            activeStores = 0;
        }

        // 更新或创建月度收入记录
        PlatformMonthlyIncome monthlyIncome = platformMonthlyIncomeMapper.getMonthlyIncome(year, month);
        if (monthlyIncome == null) {
            monthlyIncome = new PlatformMonthlyIncome();
            monthlyIncome.setYear(year);
            monthlyIncome.setMonth(month);
            monthlyIncome.setCreatedAt(LocalDateTime.now());
        }

        monthlyIncome.setTotalCommission(totalCommission);
        monthlyIncome.setTotalStores(activeStores);
        monthlyIncome.setUpdatedAt(LocalDateTime.now());

        if (monthlyIncome.getId() == null) {
            platformMonthlyIncomeMapper.insert(monthlyIncome);
        } else {
            platformMonthlyIncomeMapper.updateById(monthlyIncome);
        }
    }

    @Override
    public BaseResponse<PlatformMonthlyIncome> getMonthlyIncome(Integer year, Integer month) {
        if (year == null || month == null || month < 1 || month > 12) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        PlatformMonthlyIncome monthlyIncome = platformMonthlyIncomeMapper.getMonthlyIncome(year, month);
        return ResultUtils.success(monthlyIncome);
    }

    @Override
    public BaseResponse<List<PlatformMonthlyIncome>> getYearlyIncome(Integer year) {
        if (year == null) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        List<PlatformMonthlyIncome> yearlyIncome = platformMonthlyIncomeMapper.getYearlyIncome(year);
        return ResultUtils.success(yearlyIncome);
    }

    @Override
    public BaseResponse<BigDecimal> getTotalCommission(Integer year, Integer month) {
        if (year == null || month == null || month < 1 || month > 12) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        BigDecimal totalCommission = platformMonthlyIncomeMapper.calculateTotalCommission(year, month);
        return ResultUtils.success(totalCommission != null ? totalCommission : BigDecimal.ZERO);
    }

    @Override
    public BaseResponse<Integer> getActiveStoresCount(Integer year, Integer month) {
        if (year == null || month == null || month < 1 || month > 12) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        Integer activeStores = platformMonthlyIncomeMapper.countActiveStores(year, month);
        return ResultUtils.success(activeStores != null ? activeStores : 0);
    }
}